import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import Home from "@/views/user/Home.vue";

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: Home,
        redirect: "/recommend",//默认访问demo1子路由
        children: [
            {
                path: 'recommend',
                component: () => import("@/views/user/layout/recommend/Recommend.vue")
            }, {
                path: 'explore',
                component: () => import("@/views/user/layout/explore/Explore.vue")
            }, {
                path: 'social',
                component: () => import("@/views/user/layout/social/Social.vue")
            }, {
                path: 'message',
                component: () => import("@/views/user/layout/message/Message.vue")
            },
            {
                path: 'user',
                component: () => import("@/views/user/layout/user/User.vue")
            }
        ]
    },
    {
        path: '/login',
        component: () => import("@/views/user/Login.vue")
    },
    // ------------------------------------------------------------------
    {
        path: '/search/:p',
        component: () => import("@/views/user/layout/explore/Search.vue")
    },
    {
        path: '/history',
        component: () => import("@/views/user/layout/explore/History.vue")
    },
    {
        path: "/addEvent",
        component: () => import("@/views/user/layout/user/AddEvent.vue")
    }, {
        path: "/show/:user",
        name: 'showUser',
        component: () => import("@/views/user/layout/explore/Show.vue")
    },


    //配置后台管理主网址 --------------------------
    {
        path: "/admin/",
        component: () => import("@/views/admin/Home.vue"),
        children: [
            {
                path: 'user',
                component: () => import("@/views/admin//layout/user/List.vue")
            },
            {
                path: "event",
                component: () => import("@/views/admin/layout/event/EventList.vue")
            }
        ]
    }

]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

//添加路由守卫
//控制前端: 哪些请求可以直接访问 哪些请求需要登录后才能访问、
//如果没有登录 需强制登录
//to:要访问的地址
//from:请求来源
//next():表示放行当前请求  next("/login")强制放行到登录

//???
// router.beforeEach((to, from, next) => {
//     //定义一个数组 所有放行地址数组
//     const exclude = ['/login', '/reg', "/demo1", "/demo4"]
//     //判断访问的地址 是否存在上面存在
//     if (exclude.includes(to.path)) next()
//     else {
//         const token = localStorage.getItem("token");
//         if (!token) {  //没有登录认证的时候 强制登录
//             return next('/login')
//         }
//         //如果有登录认证的时候   则放行
//         next()
//     }
// })

export default router
